package com.hupu.jrs.widget

import android.graphics.Color
import android.text.Spannable
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.method.Touch
import android.text.style.BackgroundColorSpan
import android.text.style.ClickableSpan
import android.view.MotionEvent
import android.widget.TextView

/**
 * Created by sll on 2015/6/11.
 */
class MyMovementMethod : LinkMovementMethod() {

    private var mGray: BackgroundColorSpan? = null

    private var mIsLinkHit = false

    val isLinkHit: Boolean
        get() {
            val ret = mIsLinkHit
            mIsLinkHit = false
            return ret
        }

    override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
        if (mGray == null) {
            mGray = BackgroundColorSpan(Color.parseColor("#eaeaea"))
        }

        mIsLinkHit = false

        val action = event.action

        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
            var x = event.x.toInt()
            var y = event.y.toInt()

            /*if (DEBUG) {
                Log.d(TAG, "x = " + x + " y = " + y);
			}*/

            x -= widget.totalPaddingLeft
            y -= widget.totalPaddingTop

            /*if (DEBUG) {
                Log.d(TAG, "x = " + x + " y = " + y);
			}*/

            x += widget.scrollX
            y += widget.scrollY

            val line = widget.layout.getLineForVertical(y)
            val offset = widget.layout.getOffsetForHorizontal(line, x.toFloat())

            val spans = buffer.getSpans(offset, offset, ClickableSpan::class.java)

            /*if (DEBUG) {
                Log.d(TAG, "x = " + x + " y = " + y);
				Log.d(TAG, "line = " + line + " offset = " + offset);
				Log.d(TAG, "spans.lenth = " + spans.length);
			}*/

            if (spans.size != 0) {
                val start = buffer.getSpanStart(spans[0])
                val end = buffer.getSpanEnd(spans[0])

                mIsLinkHit = true

                if (action == MotionEvent.ACTION_DOWN) {
                    /*if (DEBUG) {
                        Log.d(TAG, "Down event detected");
					}*/

                    buffer.setSpan(mGray, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                } else if (action == MotionEvent.ACTION_UP) {
                    /*if (DEBUG) {
            Log.d(TAG, "Up event detected");
					}*/

                    spans[0].onClick(widget)

                    buffer.removeSpan(mGray)
                }

                return true
            }
        } else {
            buffer.removeSpan(mGray)
        }

        return Touch.onTouchEvent(widget, buffer, event)
    }

    companion object {

        private var sInstance: MyMovementMethod? = null

        val instance: MyMovementMethod
            get() {
                if (sInstance == null) {
                    sInstance = MyMovementMethod()
                }

                return sInstance as MyMovementMethod
            }
    }
}
